//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<CBaseVertex9.cpp>
///	@path	~/src/graphics/dx9/
///	@date	2007/10/25
///	@desc	.

#include "config/config.h"

#include "graphics/dx9/CBaseVertex9.h"

namespace xeres {

	IMPL_IOBJECT_CLASS( CBaseVertex9 );

	// CBaseVertex9
	CBaseVertex9::CBaseVertex9( IDirect3DVertexBuffer9 * buffer , size_t count , size_t stride )
		: m_pBuffer( buffer )
		, m_vertexCount( count )
		, m_stride( stride )
	{
		HRESULT hr = m_pBuffer->GetDesc( &m_desc );
		XS_ASSERT( SUCCEEDED(hr) );
	}
	// ~CBaseVertex9
	CBaseVertex9::~CBaseVertex9( void )
	{
		if( m_pBuffer )
		{
			UINT rest = m_pBuffer->Release();
			if( rest != 0 )
			{
				TRACE_ERROR( _S("CBaseVertex9::DTOR: Invalid IDirect3DVertexBuffer9 reference count.") );
				FATAL_ABORT( _S("Invalid IDirect3DVertexBuffer9 reference count.") );
			}
		}
	}
	// OnLostDevice
	bool CBaseVertex9::OnLostDevice( void )
	{
		return true;
	}
	// OnResetDevice
	bool CBaseVertex9::OnResetDevice( RefWeak<IDevice> device )
	{
		return true;
	}
	// GetCount
	size_t CBaseVertex9::GetCount( void )
	{
		return m_vertexCount;
	}
	// GetSize
	size_t CBaseVertex9::GetSize( void )
	{
		return m_desc.Size;
	}
	// IsWritable
	bool CBaseVertex9::IsWritable( void )
	{
		return !(m_desc.Pool & D3DPOOL_DEFAULT);
	}
	// IsReadable
	bool CBaseVertex9::IsReadable( void )
	{
		return !(m_desc.Pool & D3DPOOL_DEFAULT) && ( m_desc.Usage & D3DUSAGE_DYNAMIC );
	}
	// Map
	bool CBaseVertex9::Map( bool discard , ResourceMap& data )
	{
		TRACE_ERROR( _S("CBaseVertex9::Map: Not implemented.") );
		FATAL_ABORT( _S("Not implemented call.") );
		return false;
	}
	// ReadBack
	bool CBaseVertex9::ReadBack( ResourceMap& data , bool readonly )
	{
		TRACE_ERROR( _S("CBaseVertex9::ReadBack: Not implemented.") );
		FATAL_ABORT( _S("Not implemented call.") );
		return false;
	}
	// Unmap
	bool CBaseVertex9::Unmap( void )
	{
		TRACE_ERROR( _S("CBaseVertex9::Unmap: Not implemented.") );
		FATAL_ABORT( _S("Not implemented call.") );
		return false;
	}
	// GetVertexInterface
	IUnknown * CBaseVertex9::GetVertexInterface( void )
	{
		return m_pBuffer;
	}
	// GetIndexInterface
	IUnknown * CBaseVertex9::GetIndexInterface( void )
	{
		return NULL;
	}
	// GetStreamCount
	uint CBaseVertex9::GetStreamCount( void )
	{
		return 1;
	}
	// ApplyStream
	void CBaseVertex9::ApplyStream( RenderContext& rc , uint offset_vertex )
	{
		rc.GetDX9()->SetStreamSource( 0 , m_pBuffer , offset_vertex * m_stride , m_stride );
	}
	
} // namespace xeres
